<?php



namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 示例接口
 */
class Work extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // 我的记录

    public function gettodylog(){

        $user=$this->auth->getUser();

        $date=date('Y-m-d');

        $log=Db::name('workhour')
            ->where(['user_id'=>$user['id'],'date'=>$date,'status'=>0])
            ->order('id desc')->find();

        if ($log){
            $log['startime_t']=date('H:i',$log['startime']);
            $log['endtime_t']=date('H:i',$log['endtime']);
        }else{
            $log=[];
        }



        $this->success('我的记录',$log);

    }


    //打卡

    public function submits(){

        $use=$this->auth->getUser();
        $user=Db::name('user')->where(['id'=>$use['id']])->find();

        $date=date('Y-m-d');


        $h=date('H');

        if ($h<9||$h>22){

            $this->error('当前时间不能打卡');
        }


        $log=Db::name('workhour')
            ->where(['quyu_id'=>$user['quyu_id'],'date'=>$date,'status'=>0])
            ->order('id desc')->find();


        // 下班卡
        if ($log){

            $s=intval(time()-$log['startime']);

            if($s<3000){

                $this->error('时间过近！');

            }

            $hour=ceil($s/3600);





            Db::name('workhour')
                ->where(['id'=>$log['id']])
                ->update(['endtime'=>time(),'status'=>1,'hour'=>$hour]);


        //上班卡
        }else{

            Db::name('workhour')
                ->data(['startime'=>time(),'status'=>0,'date'=>date('Y-m-d'),'user_id'=>$user['id'],'quyu_id'=>$user['quyu_id']])
                ->insert();


        }



        $this->success('打卡成功');
    }




















}